package com.example.zhunongzonghe.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.zhunongzonghe.beans.ZnFarmproduceBean;
import com.example.zhunongzonghe.common.Result;
import com.example.zhunongzonghe.entity.ZnFarmSpecification;
import com.example.zhunongzonghe.entity.ZnFarmproduce;
import com.example.zhunongzonghe.service.IZnFarmproduceService;
import com.example.zhunongzonghe.service.ZnFarmSpecificationService;
import com.example.zhunongzonghe.utils.BeanCopyUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.security.Principal;
import java.time.LocalDateTime;
import java.util.List;


@RestController
@RequestMapping("/znFarmproduce")
public class ZnFarmproduceController {
    @Autowired
    private IZnFarmproduceService znFarmproduceService;
    @Resource
    private ZnFarmSpecificationService specificationService;
    /**
     * 后台分页查询全部农产品信息，以及分页搜索
     * @param pageId
     * @param pageNum
     * @param Search
     * @return
     */
    @GetMapping("/SearchFarmAll")
    public Result<Page> SearchFarmAll(@RequestParam("pageId") Integer pageId,
                                      @RequestParam("pageSize") Integer pageNum,
                                      @RequestParam("input") Object Search){
        Page<ZnFarmproduceBean> znFarmproducesbean = znFarmproduceService.SearchFarmAllInfo(pageId,pageNum,Search);
        if (pageId > znFarmproducesbean.getPages()){
            znFarmproducesbean=znFarmproduceService.SearchFarmAllInfo((int) znFarmproducesbean.getPages(),pageNum,Search);
        }
        return new Result<>(true,"成功",200,znFarmproducesbean);
    }

    /**
     * 农产品页全部已上架数据
     * @param pageId
     * @param pageNum
     * @param Search
     * @return
     */
    @GetMapping("/FindAllProduce")
    public Result<Page> FindAllProduce(@RequestParam("pageId") Integer pageId,
                                      @RequestParam("pageSize") Integer pageNum,
                                      @RequestParam("input") Object Search){
        Page<ZnFarmproduceBean> znFarmproducesbean = znFarmproduceService.FindAllProduceInfo(pageId,pageNum,Search);
        return new Result<>(true,"成功",200,znFarmproducesbean);
    }

    //添加农产品
    @PostMapping("/addFarm")
    public Result addFarm(@RequestBody ZnFarmproduceBean znFarmproduceBean){
        if (znFarmproduceBean !=null){
            ZnFarmproduce znFarmproduce = new ZnFarmproduce();
            BeanUtils.copyProperties(znFarmproduceBean,znFarmproduce);
            znFarmproduce.setCreateTime(LocalDateTime.now());
            znFarmproduceService.save(znFarmproduce);
            znFarmproduceBean.getSpecificationBeans().forEach(specBeans->{
                ZnFarmSpecification specification = new ZnFarmSpecification();
                specification.setZnFarmId(znFarmproduce.getFarmId());
                specification.setCreateTime(LocalDateTime.now());
                specification.setZnSpecTitle(specBeans.getZnSpecTitle());
                specification.setZnSpecUnit(specBeans.getZnSpecUnit());
                specification.setZnSpecPrice(specBeans.getZnSpecPrice());
                specification.setInventory(specBeans.getInventory());
                specificationService.save(specification);
            });
            return new Result<>(true,"添加成功",200);
        }else {
            return new Result<>(false, "添加失败", 400);
        }
    }
    //编辑农产品信息
    @PutMapping("/editorParm")
    public Result<ZnFarmproduce> editorParm(@RequestBody ZnFarmproduceBean znFarmproduceBean){
        ZnFarmproduce znFarmproduce = BeanCopyUtils.copyBean(znFarmproduceBean,ZnFarmproduce.class);
        znFarmproduce.setUpdateTime(LocalDateTime.now());
        znFarmproduceService.updateById(znFarmproduce);
        List<ZnFarmSpecification> specificationList =
                BeanCopyUtils.copyBeanList(znFarmproduceBean.getSpecificationBeans(),ZnFarmSpecification.class);
        for (ZnFarmSpecification spec: specificationList
             ) {
            spec.setZnFarmId(znFarmproduceBean.getFarmId());
            spec.setCreateTime(LocalDateTime.now());
            spec.setUpdateTime(LocalDateTime.now());
            specificationService.saveOrUpdate(spec);
        }
        return new Result<>(true,"编辑成功",200);
    }

    /**
     * 删除规格
     * @return
     */
    @DeleteMapping("/deleteSpecif/{specId}")
    public Result deleteSpecifById(@PathVariable Integer specId){
        if (specId==null) return new Result(false,"数据异常",200);
        specificationService.removeById(specId);
        return new Result(true,"成功",200);
    }
    //删除农产品
    @DeleteMapping("/deleteFarm/{id}")
    public Result<ZnFarmproduce> deleteFarm(@PathVariable Integer id){
        znFarmproduceService.removeById(id);
        LambdaQueryWrapper<ZnFarmSpecification> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnFarmSpecification::getZnFarmId,id);
        List<ZnFarmSpecification> specificationList = specificationService.list(queryWrapper);
        specificationService.removeBatchByIds(specificationList);
        return new Result<>(true,"删除成功",200);
    }

    /**
     * 首页农产品显示
     * @return
     */
    @GetMapping("/SearchFarm")
    public Result<List<ZnFarmproduceBean>> SearchFarm(){
        List<ZnFarmproduceBean> beanList = znFarmproduceService.HomeFarmAll();
        return new Result<>(true,"查询成功",200,beanList);
    }
    /**
     *首页农产品热门显示
     */
    @GetMapping("/getProduceTop")
    public Result getProduceTop(){
        List<ZnFarmproduceBean> beanList = znFarmproduceService.HomeProduceTop();
        return new Result(true,"成功",200,beanList);
    }
    /**
     * 查询单个农产品信息
     * @param farmId
     * @return
     */
    @GetMapping("/DisInfoFarm/{farmId}")
    public Result<ZnFarmproduceBean> DisInfoFarm(@PathVariable Integer farmId, Principal principal){
        ZnFarmproduceBean znFarmproduceBean =  znFarmproduceService.DisInfoFarm(farmId, principal.getName());
        return new Result<>(true,"查询成功",200,znFarmproduceBean);
    }
    /**
     * 未登录查询单个农产品信息
     * @param farmId
     * @return
     */
    @GetMapping("/NotLogInInfoFarm/{farmId}")
    public Result<ZnFarmproduceBean> NotLogInInfoFarm(@PathVariable Integer farmId){
        ZnFarmproduceBean znFarmproduceBean =  znFarmproduceService.NotLogInInfoFarm(farmId);
        return new Result<>(true,"查询成功",200,znFarmproduceBean);
    }

    /**
     * 分页查询当前分类的所有数据
     * @param sort
     * @return
     */
    @GetMapping("/SortSearchFarm")
    public Result<Page> SortSearchFarm(@RequestParam("pageId") Integer pageId,
                                       @RequestParam("pageSize") Integer pageNum,
                                       @RequestParam("sort") String sort){
        Page<ZnFarmproduceBean> znFarBeanPage = znFarmproduceService.SortSearchFarm(pageId,pageNum,sort);
        return new Result<>(true,"查询成功",200,znFarBeanPage);
    }
}